package com.cwz.controller;

import com.cwz.service.DeepSeekService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.concurrent.CompletableFuture;

@RestController
@Slf4j
@RequestMapping("/api/deepseek")
public class DeepSeekController {

    @Autowired
    private DeepSeekService deepSeekService;

    @GetMapping(value = "/chat-stream", produces = "text/event-stream")
    public SseEmitter chatStream(@RequestParam String userMessage) {
        SseEmitter emitter = new SseEmitter();
        log.info("调用deepseekAPI");
        // 异步处理流式输出
        CompletableFuture.runAsync(() -> {
            try {
                deepSeekService.sendStreamRequest(userMessage, emitter);
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        });

        return emitter;
    }
}